package com.itheimajdbc;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//JDBC API 详解：Statement
public class jdbcdemo_ResultSet {

   //执行DQL语句
   @Test
    public void testDML() throws Exception{
       //注册驱动
       Class.forName("com.mysql.cj.jdbc.Driver");
       //2.获取连接：(如果是本地的mysql并且端口号默认的3306可以简化书写)
       String url = "jdbc:mysql://localhost:3306/mysql";
       String username = "root";
       String password = "123456";
       Connection conn = DriverManager.getConnection(url,username,password);
       //3.定义sql
       String sql = "Select * from demo";
       //4.获取Statement对象
      Statement stmt = conn.createStatement();
      //5.执行sql
      ResultSet rs = stmt.executeQuery(sql);
      //6.处理结果，遍历rs中的所有数据
      //6.1光标向下移动，并且判断当前行是否有数据
      while(rs.next()){
         //6.2获取数据 getXxx（）
         int id = rs.getInt(1);
         String name=rs.getString(2);
         double money = rs.getDouble(3);

         System.out.println(id);
         System.out.println(name);
         System.out.println(money);
      }
      //7.释放资源
      rs.close();
      stmt.close();
      conn.close();
   }
}